package com.atguigu.dga.governance.assessor;

import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;
import com.atguigu.dga.governance.bean.GovernanceMetric;
import com.atguigu.dga.meta.bean.TableMetaInfo;
import org.apache.arrow.flatbuf.Int;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;

public  abstract class Assessor {
    public final GovernanceAssessDetail assess(AssessParam assessParam) {
        String assessDate = assessParam.getAssessDate();
        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();
        GovernanceMetric governanceMetric = assessParam.getGovernanceMetric();
        // 默认值
        GovernanceAssessDetail governanceAssessDetail = new GovernanceAssessDetail();
        governanceAssessDetail.setTableName(tableMetaInfo.getTableName());
        governanceAssessDetail.setSchemaName(tableMetaInfo.getSchemaName());
        governanceAssessDetail.setMetricId(governanceMetric.getId().toString());
        governanceAssessDetail.setMetricName(governanceMetric.getMetricName());
        governanceAssessDetail.setAssessDate(assessDate);
        governanceAssessDetail.setCreateTime(new Date());
        governanceAssessDetail.setGovernanceType(governanceMetric.getGovernanceType());
        governanceAssessDetail.setTecOwner(tableMetaInfo.getTableMetaInfoExtra().getTecOwnerUserName());
        governanceAssessDetail.setAssessScore(BigDecimal.TEN); //默认给10分
        //核心方法 给分，给扣分的原因
        try {
            this.checkProblem(  assessParam,governanceAssessDetail);
        }catch (Exception e){
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);  //把报错内容写到string中

            governanceAssessDetail.setIsAssessException("1");
            Integer endIndex =Math.min(stringWriter.toString().length()-1,2000);
            governanceAssessDetail.setAssessExceptionMsg(stringWriter.toString().substring(0,endIndex));
        }
        return   governanceAssessDetail;

    }

    public abstract void  checkProblem(AssessParam assessParam,GovernanceAssessDetail governanceAssessDetail) throws ParseException;
}
